home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2710 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.8 KB

  1. Path: eua.ericsson.se!usenet
  2. From: euahjn@eua.ericsson.se (Henrik Johansson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Passing a pointer to a class function
  5. Date: 19 Jan 1996 07:08:38 GMT
  6. Organization: Ericsson Telecom Systems Labs, Stockholm, Sweden
  7. Distribution: world
  8. Message-ID: <4dng1m$q2u@euas20.eua.ericsson.se>
  9. References: <Pine.SUN.3.91.960117160141.13519A-100000@sasabe.acms.arizona.edu>
  10. Reply-To: euahjn@eua.ericsson.se
  11. NNTP-Posting-Host: euas31i2c37.eua.ericsson.se
  12. NNTP-Posting-User: euahjn
  13.  
  14. In article <Pine.SUN.3.91.960117160141.13519A-100000@sasabe.acms.arizona.edu>, Kenton White <jwhite@sasabe.acms.arizona.edu> writes:
  15. > I have a library subroutine that takes as an argument a pointer to a 
  16. > function.  I want to pass instead a pointer to a class function.  The 
  17. > class looks like this:
  18. > class device {
  19. > // ...
  20. > public:
  21. > //...
  22. > void derivs(float, complex*, complex*)
  23. > };
  24. > The subroutine expects a pointer to a function like this:
  25. > rk(void (*)(float, void*, void*))
  26. > I pass the class function like this:
  27. > device laser;
  28. > rk(void (*)(float,void*, void*))laser.derivs;
  29. > When it compiles it gives me an anachronism warning and then crashes 
  30. > with a memory fault on execution.  I have developed a temporary fix by 
  31. > declaring a seperate function in the main program called derivs that 
  32. > calls the class function derivs.  This works but is silly.  Is there a 
  33. > way to pass the pointer to the class function?
  34. > Kenton
  35.  
  36. It is less silly to declare a static `wrapper' function inside the
  37. class, not in the main program;
  38. `static void lessSillyWrapper(float, void*, void*);'
  39. Then you provide it to rk by `rk(device::lessSillyWrapper)'
  40. Then, of course, lessSillyWrapper somehow gets the object reference to
  41. some `device' object and calls the `derivs' member function. See
  42. the FAQ section 18.
  43.  
  44.